package com.maisha.Service.cms;

import java.io.StringWriter;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.maisha.Dao.cms.CMSPowerDaoImp;
import com.maisha.Service.IService.ICMSPowerService;
import com.maisha.bean.AuditPower;
import com.maisha.form.PowerForm;
import com.maisha.utils.JSONUtils;
import com.maisha.utils.SuitConstant;
import com.maisha.utils.UUIDUtils;

@Service(value="cmsPowerServiceImp")
public class CMSPowerServiceImp implements ICMSPowerService {
	
	private static final Logger log = Logger.getLogger(CMSPowerServiceImp.class);
	@Resource
	private CMSPowerDaoImp cmsPowerDaoImp; 
	
	@Override
	public List<AuditPower> getAuditPowerList() {
		String sql = "select id,name,code from audit_power ap where ap.is_valid=?";
		List<AuditPower> result = null;
		try {
			result = cmsPowerDaoImp.getResultList(sql, AuditPower.class, SuitConstant.isValid.VALID.getValue());
		} catch (SQLException e) {
			log.error("获得 权限列表信息失败！");
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public void addAuditPower(AuditPower auditpower) {
		auditpower.setId(UUIDUtils.getUUId());
		try {
			cmsPowerDaoImp.addObject(AuditPower.class, auditpower, false);
		} catch (Exception e) {
			log.error("添加 权限信息失败！");
			e.printStackTrace();
		}
	}

	@Override
	public String getAuditPowerByLevel(PowerForm powerForm) {
		String sql = "select id,name,code from audit_power ap where ap.is_valid=? and ap.level=? order by create_time_ms";
		List<AuditPower> result = null;
		StringWriter sb = new StringWriter();
		try {
			result = cmsPowerDaoImp.getResultList(sql, AuditPower.class, SuitConstant.isValid.VALID.getValue(),powerForm.getLevel());
			JSONUtils.list2Json(result, sb);
		} catch (SQLException e) {
			log.error("获得 权限列表信息失败！");
			e.printStackTrace();
		}
		return sb.toString();
		
	}
	
}
